package com.qbkj.base_lib.adapter;

import android.support.annotation.Nullable;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by St on 2018/4/19.
 */

public class LoadingAdapterValue<T> {
    private ReflectField mReflectField;
    private SparseArray<FieldShadow[]> mValueCache;
    private List mData;
    private String mImageHeadUrl;

    public LoadingAdapterValue(@Nullable List data) {
        mValueCache = new SparseArray<>();
        mReflectField = ReflectField.getInstance();
        this.mData=data;
    }

    public List<View> getViews(View view) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                allchildren.add(viewchild);
                allchildren.addAll(getViews(viewchild));
            }
        }
        return allchildren;
    }


    public synchronized void setValue(final View itemView, final int position, final T data) {
        //从缓存中拿数据
        List<View> views = getViews(itemView);
        if (mValueCache.size() == mData.size()) {
            for (int i = 0; i < views.size(); i++) {
                final View view = views.get(i);
                String tag = (String) view.getTag();
                if (tag != null) {
                    String value = getValue(position, i);
                    bindingValue(tag, view, value);
                }
            }
            return;
        }
        //反射出数据
        FieldShadow fieldShadows[] = new FieldShadow[views.size()];
        for (int i = 0; i < views.size(); i++) {
            final View view = views.get(i);
            String tag = (String) view.getTag();
            if (tag != null) {
                String value = mReflectField.getFieldName(position, i, data, tag);
                if (value != null) {
                    FieldShadow fieldShadow = new FieldShadow();
                    fieldShadow.setPosition(position);
                    fieldShadow.setIndex(i);
                    fieldShadow.setValue(value);
                    fieldShadow.setData(data);
                    fieldShadows[i] = fieldShadow;
                }
                bindingValue(tag, view, value);
            }
        }
        if (fieldShadows.length != 0) mValueCache.put(position, fieldShadows);
    }

    private void bindingValue(String tag, View view, String value) {
        if (value == null) return;
        if (view instanceof TextView) {
            ((TextView) view).setText(value);
        }

        if (value != null && view instanceof ImageView) {
            if (mImageHeadUrl != null) Glide.with(view.getContext()).load(mImageHeadUrl + value).into((ImageView) view);
            else Glide.with(view.getContext()).load(value).into((ImageView) view);
        }

        if (value != null && view instanceof ProgressBar) {
            int progress;
            try {
                progress = Integer.parseInt(value);
                ((ProgressBar) view).setProgress(progress);
            } catch (NumberFormatException e) {
                throw new NumberFormatException("view tag=" + tag + "value 类型转换错误 非int型" + e.toString());
            }
        }
    }


    //设置网络图片头
    public void setImageHeadUrl(String mImageHeadUrl) {
        this.mImageHeadUrl = mImageHeadUrl;
    }

    private String getValue(int position, int index) {
        try {
            FieldShadow[] fieldShadows = mValueCache.get(position);
            return fieldShadows[index].getValue();
        } catch (NullPointerException e) {
            return null;
        }
    }

}
